class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def hasPathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: bool
        """
        if not root:
            return False

        def search(node, count):
            val = count + node.val
            if not node.left and not node.right:
                if val == targetSum:
                    return True
                else:
                    return False
            flag = False
            if node.left:
                flag = search(node.left, val)
            if flag:
                return True
            if node.right:
                return search(node.right, val)
            else:
                return False

        return search(root, 0)